﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved

using System.Runtime.Serialization;
using System.Collections.Generic;
using System.IO;
using System;
using System.Linq;
using System.Reflection;
using MichMan.Utilities;

namespace Subsonic.Data
{
    [DataContract]
    public class SubsonicServer
    {
        public SubsonicServer()
        {
            Name = String.Empty;
            ServerAddress = String.Empty;
            Username = String.Empty;
            Password = String.Empty;
        }

        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string ServerAddress { get; set; }
        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public bool Jukebox { get; set; }
    }

    public enum BitRate
    {
        BitRateUnlimited = 0,
        BitRate96Kbps = 96,
        BitRate128Kbps = 128,
        BitRate160Kbps = 160,
        BitRate320Kbps = 320,
    }

    public enum PlayMode
    {
        Stream,
        Download,
        Offline,
        Jukebox
    }

    [DataContract]
    public class SubsonicSettings
    {
        public SubsonicSettings()
        {
            WiFiBitRate = BitRate.BitRateUnlimited;
            MobileBitRate = BitRate.BitRateUnlimited;
            CacheSize = int.MaxValue;
            SongsToPreLoad = int.MaxValue;

            Servers = new List<SubsonicServer>();
            CurrentServer = 1;
            Servers.Add(new SubsonicServer()
            {
                Name = "Subsonic demo",
                ServerAddress = "http://subsonic.org/demo",
                Username = "android-guest",
                Password = "guest",
            });
            Servers.Add(new SubsonicServer()
            {
                Name = "Unused",
                ServerAddress = "http://yourhost",
            });

            FileManager.Instance.StorageCleared += new EventHandler(Instance_StorageCleared);
        }

        void Instance_StorageCleared(object sender, EventArgs e)
        {
            Save();
        }


        public SubsonicServer ActiveServer
        {
            get
            {
                if (CurrentServer < 0 || CurrentServer >= Servers.Count)
                {
                    return new SubsonicServer();
                }
                return Servers[CurrentServer];
            }
        }

        [DataMember]
        public List<SubsonicServer> Servers { get; set; }

        [DataMember]
        public PlayMode PlayMode { get; set; }

        [DataMember]
        public int CurrentServer { get; set; }

        [DataMember]
        public int SongsToPreLoad { get; set; }

        [DataMember]
        public BitRate WiFiBitRate { get; set; }

        [DataMember]
        public BitRate MobileBitRate { get; set; }

        [DataMember]
        public int CacheSize { get; set; }

        private static SubsonicSettings _instance = null;
        public static SubsonicSettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = Settings<SubsonicSettings>.Instance;
                }
                return _instance;
            }
        }

        public static void Save()
        {
            Settings<SubsonicSettings>.Save();
        }
    }
}